<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <style>
        body{ margin: 0 }
        body:after{ content:"."; height:0; visibility:hidden; display:block; clear:both; }
        .OrgBox{
            font-size:12px;
            padding:5px 5px 5px 5px;
            clear:left;
            float:left;
            text-align:center;
            position:absolute;
            background-color: #64b5f6;
            border-radius: 5px;
            border: 1px solid #2196f3;
            width:90px;
            height:125px;
        }
        .OrgBox img{
            width:60px;
            height:70px;
        }
        .OrgBox div{
            color:#333;
            font-weight:800;
        }
        </style>
    </head>
    <body>
        
    </body>
    <script src="../../Scripts/organization.js"></script>
    <script>
        var json = [];

        document.addEventListener('click',function(e){
            if(e.target.className == 'OrgBox'){
                clicknode(e.target);
            }
            if(e.target.parentNode.className == 'OrgBox'){
                clicknode(e.target.parentNode);
            }
            if(e.target.parentNode.parentNode.className == 'OrgBox'){
                clicknode(e.target.parentNode.parentNode);
            }
        })
        function clicknode(event){
            for(var key in json){
                if(json[key].name == event.title){
                    parent.loaddetailmsg(json[key],event);
                }
            }
        }
        function jsonchange(data,parentid){
		debugger
            for(var i=0;i<data.length;i++){
                if(data[i].entity.type.toLocaleLowerCase() == 'mestracenode'){
                    var curdata = {};
                    curdata['id'] = data[i].nodeId;
                    if(parentid == null){parentid = 0}
                    curdata['pid'] = parentid;
                    curdata['name'] = "node_"+data[i].nodeId;
                    curdata['description'] = data[i].entity;
                    curdata['other_msg'] = [];
                    json.push(curdata);
                    if(data[i].children.length>0){
                        jsonchange(data[i].children,data[i].nodeId);
                    }
                }else{
                    for(var j=0;j<json.length;j++){
                        if(json[j].id == parentid){
                            //wym 2017/12/28，如果是仓库物料，需要将关系上的type属性设置到物料的属性里面
                            if(data[i].entity.type.toLocaleLowerCase() == "meswarehouseitem")
                            {
                                if(data[i].relation.properties["type"] == "1")
                                {
                                    data[i].entity.properties["物料耗用类型"] = "当前物料";
                                }
                                else if(data[i].relation.properties["type"] == "0")
                                {
                                    data[i].entity.properties["物料耗用类型"] = "消耗子件";
                                }
                                data[i].entity.properties["数量"] = data[i].relation.properties["usequantity"];
                            }
                            json[j].other_msg.push(data[i].entity);

                        }
                    }
                }
            }
        }
        function createtree(data){
            //  console.log(JSON.stringify(data));
            var qcpProcess = eval("JSON.parse(decodeURI(sessionStorage.qcpProcess))");
            for(var j=0;j<data.length;j++){
                var reportsheet = {};
                var warehouseitem = {};
                var state = 0;//默认没有报工单
                for(var k=0;k<data[j].other_msg.length;k++){
                    if(data[j].other_msg[k].type.toLocaleLowerCase() == 'mesreportsheet'){
                         reportsheet = data[j].other_msg[k];
                         state = 1;
                    }
                    else if(data[j].other_msg[k].type.toLocaleLowerCase() == 'meswarehouseitem'){
                        warehouseitem = data[j].other_msg[k];
                    }
                }
                eval("var node_" + data[j].id + "= new OrgNode();");
                if(warehouseitem.properties != null ){
                    eval("node_"+data[j].id+".customParam.EmpName = '"+ warehouseitem.properties.name+"'");
                    eval("node_"+data[j].id+".customParam.EmpName += '"+ warehouseitem.properties.processname+"'");
                }else{
                    eval("node_"+data[j].id+".customParam.EmpName = ''");
                }
                //data[j].description:是追溯点对象
                if(data[j].description.properties != null && data[j].description.properties.creationdate != null){
                    eval("node_"+data[j].id+".customParam.department = '"+ data[j].description.properties.creationdate+"'");
                }else{
                    eval("node_"+data[j].id+".customParam.department = ''");
                }
                eval("node_"+data[j].id+".Text = 'node_"+ data[j].id +"'");
                if(state == 1){
                    eval("node_"+data[j].id+".customParam.tip = '报工单'");
                }else{
                    eval("node_"+data[j].id+".customParam.tip = ''");
                }
            }
            for(var k=0;k<data.length;k++){
                if(data[k].pid == 0)continue
                eval("node_"+data[k].pid+".Nodes.Add("+ data[k].name +")");
            }
            var OrgShows=new OrgShow(eval(""+data[0].name));
            OrgShows.Top=50;
            OrgShows.Left=50;
            OrgShows.IntervalWidth=20;
            OrgShows.IntervalHeight=20;
            
            OrgShows.BoxTemplet="<div id=\"{Id}\" title=\"{Text}\" class=\"OrgBox\" ><div style='height:100%;overflow:hidden'><span>{EmpName}</span><div>{department}</div><div style='position:absolute;top:-15px;right:0;color:#e4393c'>{tip}</div></div></div>"
            OrgShows.Run()
            // console.log(json);
        }
       
        var href = window.location.href;
        var str = eval("JSON.parse(decodeURI(sessionStorage."+href.split('?')[1]+"))")
        jsonchange([str]);
        createtree(json);

        
        
    </script>
</html>